package com.cn.demo.service.impl;

import com.cn.demo.entity.*;
import com.cn.demo.mapper.*;
import com.cn.demo.service.DesignTeamService;
import com.cn.demo.service.EvaluateService;
import com.cn.demo.service.ExperimentService;
import com.cn.demo.service.PageInfoUtilServer;
import com.cn.demo.utils.Result;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class EvaluateServiceImpl implements EvaluateService {
    @Autowired
    private EvaluateMapper evaluateMapper;

    @Autowired
    private PageInfoUtilServer pageInfoUtilServer;

    @Autowired
    private ExperimentMapper experimentMapper;

    @Autowired
    private DesignMapper designMapper;

    @Autowired
    private InternshipMapper internshipMapper;

    @Autowired
    private DesignTeamMapper designTeamMapper;

    /**
     * 可实验
     * @param page
     * @param limit
     * @param organizationId
     * @param experimentId
     * @return
     */
    public Result getOrganizationStudentExperimentEvaluate(Integer page, Integer limit, String organizationId, String experimentId,String studentId, String studentName) {
        PageHelper.startPage(page, limit);
        List<Achievement> achievementList = evaluateMapper.organizationStudent(organizationId,studentId,studentName);
        Map<String, Object> map = pageInfoUtilServer.currency(achievementList);
        achievementList = (List) map.get("list");
        for (Achievement achievement : achievementList) {
            String studentIdNow = achievement.getStudent().getStudentId();
            Evaluate evaluate = evaluateMapper.getEvaluate(studentIdNow, experimentId);
            if (evaluate == null) {
                evaluate = new Evaluate();
                evaluate.setRank("无");
                achievement.setEvaluate(evaluate);
            }
            achievement.setEvaluate(evaluate);
            Experiment experiment = experimentMapper.getByExperimentIdAndStudentId(experimentId, studentIdNow);
            achievement.setExperiment(experiment);
        }
        return Result.success(map);
    }

    /**
     * 课程设计
     * @param page
     * @param limit
     * @param organizationId
     * @param curriculumDesignId
     * @param studentId
     * @param studentName
     * @return
     */
    @Override
    public Result getOrganizationStudentDesignEvaluate(Integer page, Integer limit, String organizationId, String curriculumDesignId,String studentId, String studentName) {
        PageHelper.startPage(page, limit);
        List<Achievement> achievementList = evaluateMapper.organizationStudent(organizationId,studentId,studentName);
        Map<String, Object> map = pageInfoUtilServer.currency(achievementList);
        achievementList = (List) map.get("list");
        for (Achievement achievement : achievementList) {
            String studentIdNow = achievement.getStudent().getStudentId();
            System.out.println("studentIdNow=="+studentIdNow);
            System.out.println("curriculumDesignId=="+curriculumDesignId);
            Evaluate evaluate = getEvaluate(studentIdNow, curriculumDesignId);
            achievement.setEvaluate(evaluate);
            Design design = designMapper.getByCurriculumDesignIdAndStudentId(studentIdNow, curriculumDesignId);
            achievement.setDesign(design);
        }
        return Result.success(map);
    }

    /**
     * 课程实习
     * @param page
     * @param limit
     * @param organizationId
     * @param curriculumInternshipId
     * @return
     */
    @Override
    public Result getOrganizationStudentInternshipEvaluate(Integer page, Integer limit, String organizationId, String curriculumInternshipId,String studentId, String studentName) {
        PageHelper.startPage(page, limit);
        List<Achievement> achievementList = evaluateMapper.organizationStudent(organizationId,studentId,studentName);
        Map<String, Object> map = pageInfoUtilServer.currency(achievementList);
        achievementList = (List) map.get("list");
        for (Achievement achievement : achievementList) {
            String studentIdNow = achievement.getStudent().getStudentId();
            Evaluate evaluate = getEvaluate(studentIdNow, curriculumInternshipId);
            achievement.setEvaluate(evaluate);
            Internship internship = internshipMapper.getInternshipByStudentIdAndCurriculumInternshipId(studentIdNow, curriculumInternshipId);
            achievement.setInternship(internship);
        }
        return Result.success(map);
    }

    /**
     * 课程设计组队
     * @param page
     * @param limit
     * @param organizationId
     * @param designId
     * @param subject
     * @return
     */
    @Override
    public Result getOrganizationStudentDesignTeamEvaluate(Integer page, Integer limit, String organizationId,String curriculumDesignId,String  designId,String subject) {
        PageHelper.startPage(page, limit);
        List<Design> designList = designTeamMapper.getOrganizationStudentDesignTeamEvaluate(organizationId,designId,subject);
        Map<String, Object> currency = pageInfoUtilServer.currency(designList);
        designList = (List<Design>) currency.get("list");
        for (Design design : designList) {
            for (DesignTeam designTeam : design.getDesignTeamList()) {
                Evaluate evaluate = getEvaluate(designTeam.getStudent().getStudentId(), curriculumDesignId);
                designTeam.getStudent().setEvaluate(evaluate);
            }
        }
        return Result.success(currency);
    }

    @Override
    public Result evaluateByStudentByCurriculumType(Evaluate evaluate) {
        if(evaluateMapper.insert(evaluate)>0){
            return Result.success();
        }
        return Result.error();
    }


    public Evaluate getEvaluate(String studentId, String curriculumExperimentId) {
        Evaluate evaluate = evaluateMapper.getEvaluate(studentId, curriculumExperimentId);
        if (evaluate == null) {
            evaluate = new Evaluate();
            evaluate.setRank("无");
        }
        return evaluate;
    }

}